// eslint-disable-next-line no-unused-vars
import shopService from './../../api/shop'
const state = {
  all: []
}

const getters = {}

const actions = {
  // 获取全部商品
  getAllProducts ({ commit }) {
    shopService.getProducts(products => {
      commit('setProducts', products)
    })
  }
}

const mutations = {
  setProducts (state, products) {
    state.all = products
  },
  decrementProductInventory (state, { id }) {
    const product = state.all.find(item => item.id === id)
    // 因为加入购物车之前判断库存必须大于0，这边可以不用判断
    product.inventory--
  }
}
// namespaced 千万别写错，不能少个d，否则报错 unknown action type
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
